package Homework;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*修改文件中的数据
假设文件中的数据是 :2-1-9-4-7-8
对数据进行排序了，然后把数据写回文件
1-2-4-7-8-9
用流式表达式来做.*/
public class work05 {
    public static void main(String[] args) throws Exception {
        BufferedWriter out=new BufferedWriter(new FileWriter("paixu.txt"));
        FileReader in=new FileReader("paixu.txt");
        out.write("2-1-9-4-7-8");
        out.close();
        ArrayList<Integer> list=new ArrayList<>();
        int ch=0;
        while((ch=in.read())!=-1){
            if(Character.isDigit((char)ch)){
                list.add(Character.getNumericValue((char)ch));
            }
        }
        StringBuilder strbuil=new StringBuilder();
        list.stream().sorted().forEach(t->strbuil.append(t).append("-"));
        strbuil.deleteCharAt(strbuil.length()-1);
        FileWriter write=new FileWriter("paixu.txt",true);
        write.write("\r"+String.valueOf(strbuil));
        write.close();
    }
}
